package com.itheima.health.controller;

import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.resources.MessageConstant;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.utils.resources.UploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(tags = "套餐模块管理")
@RestController
public class SetmealController {
    @Autowired
    private RedisTemplate redisTemplate;

    //    @Reference
//    private CheckGroupService service;

    @Reference
    private SetmealService service;

    @PostMapping("setmeal/upload")
    @ApiOperation(value = "套餐图片上传", notes = "图片上传到阿里云oss")
    public Result upload(@RequestParam("imgFile") MultipartFile file) {
        try {
            String name = file.getOriginalFilename();
            String uuidName = UploadUtils.generateRandonFileName(name);
            AliyunUtils.uploadMultiPartFileToAliyun(file.getBytes(), uuidName);
            // 保存到redis数据库中
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_RESOURCES,uuidName);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,uuidName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    //checkgroup/findCheckItemIdsByGroupId
    @GetMapping("setmeal/findCheckGruopIdsBySetmealId")
    @ApiOperation(value = "查询中间表方法", notes = "根据套餐的Id查询对应的检查组id")
    public Result findCheckItemIdsByGroupId(@RequestParam("id") int id) {
        try {
            List<Integer> list = service.findCheckGruopIdsBySetmealId(id);
            return  new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }

    }


//    @GetMapping("checkitem/findAll")
//    @ApiOperation(value = "查询检查项方法", notes = "查询所有的检查项列表信息")
//    public Result findAll() {
//        try {
//            List<CheckItem> list = service.findAll();
//            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
//        }
//    }
//
    @PostMapping("setmeal/add")
    @ApiOperation(value = "添加检查组方法", notes = "添加表单检查组列表信息")
    public Result add(@RequestBody SetmealDTO setmealDTO) {
        try {
            service.add(setmealDTO);
            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }
//
    @PostMapping("setmeal/findPage")
    @ApiOperation(value = "分页查询", notes = "根据分页条件查询分页数据")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult page = service.findPage(queryPageBean);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
//
//    @DeleteMapping("checkgroup/deleteCheckItemById")
//    @ApiOperation(value = "删除检查项", notes = "根据id逻辑删除检查项")
//    public Result deleteCheckItemById(@RequestParam("id") Integer id){
//        try {
//            CheckGroup checkGroup = new CheckGroup();
//            checkGroup.setId(id);
//            checkGroup.setIs_delete(1);
//            service.updateById(checkGroup);
//            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
//        }
//    }
}
